package com.eascs.ant.data.service;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.eascs.ant.common.service.BaseSevice;
import com.eascs.ant.data.dao.MongoDbDao;
import com.eascs.ant.data.entity.hr.HrFavorite;
import com.eascs.ant.data.entity.hr.HrMenu;
import com.eascs.ant.data.entity.hr.HrSearchForm;
import com.eascs.ant.data.mongo.entity.MongoPageQuery;
import com.eascs.site.page.PaginationInfo;

@Service
public class HrService extends BaseSevice{
	
	@Inject
	MongoDbDao mongodbDao;
	
	/**
	 * 查简历
	 * @param HrSearchForm
	 * @return
	 */
	public PaginationInfo queryByPage(HrSearchForm form){
		int pageIndex = form.getPageIndex();
		int pageSize = form.getPageSize();
		MongoPageQuery<Object> mpq = new MongoPageQuery<>(Object.class, pageSize, pageIndex);
		//1,过滤条件
		Map<String, Object> params = new HashMap<String, Object>();
		if(form.getKeyword() != null)
			params.put("关键词", form.getKeyword());
		if(form.getFunction() != null)
			params.put("职能", form.getFunction());
		if(form.getUpdate_time() != null)
			params.put("简历更新时间", form.getUpdate_time());
		if(form.getArea() != null)
			params.put("期望工作地区", form.getArea());
		mpq.addParams(params);
		//2,指定返回与不返回字段
		String[] returnFields = new String[]{"ID","学历","工作年限","company","关键词","简历更新时间","简历链接"};
		String[] noReturnFields = new String[]{"_id"};
		mpq.setReturnFields(returnFields);
		mpq.setNoReturnFields(noReturnFields);
		mpq.setCollectionName("51job");
		
		PaginationInfo paginationInfo = mongodbDao.findDataArrayWithPage(mpq);
		
		return paginationInfo;
	}
	

	public Map<String, Object> queryById(String id){
		Map<String, Object> filter = new HashMap<String, Object>();
		filter.put("ID", id);
		return mongodbDao.findOne(filter ,"51job");
	}
	
	public HrFavorite insertOrUpdate(HrFavorite favorite){
		return mongodbDao.insertOrUpdateFavorite(favorite);
	}
	
	public void delFavorite(String account , String id){
		mongodbDao.delFavorite(account, id);
	}
	
	public HrFavorite queryFavorite(String account){
		Map<String, Object> filter = new HashMap<String, Object>();
		filter.put("account", account);
		filter = mongodbDao.findOne(filter ,"hr_favorite");
		HrFavorite fr = new HrFavorite();
		fr.setAccount(account);
		List<HrMenu> list = new LinkedList<HrMenu>();
		for(String key : filter.keySet()){
			if(key.equals("account"))
				continue;
			String value = String.valueOf(filter.get(key));
			HrMenu menu = JSON.parseObject(value, HrMenu.class);
			list.add(menu);
		}
		fr.setMenuList(list);
		return fr;
	}
}

